namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    public class IndicatorBullandBearIndex : BaseIndicator
    {
        private string paramPreFix = Common.GetGuid();
        private LineShape tlsBbi;

        public IndicatorBullandBearIndex()
        {
            base.IndType = IndicatorType.BBI;
            base.IndName = "BBI";
            base.IndDesc = "多空指数";
            base.IndPeriod.Add(new IndicatorPeriod(0, "N1", 3.0));
            base.IndPeriod.Add(new IndicatorPeriod(1, "N2", 6.0));
            base.IndPeriod.Add(new IndicatorPeriod(2, "N3", 12.0));
            base.IndPeriod.Add(new IndicatorPeriod(3, "N4", 24.0));
            base.SourceDic["Close"] = "";
            base.fields = new string[] { "BBI" };
        }

        public override bool Contains(object obj)
        {
            return (obj == this.tlsBbi);
        }

        public override double[] Get(int r)
        {
            double num = 0.0;
            List<double> list = new List<double>();
            double curValue = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Close"]]);
            double[] numArray = new double[1 + base.IndPeriod.Count];
            for (int i = 0; i < base.IndPeriod.Count; i++)
            {
                IndicatorPeriod period = base.IndPeriod[i];
                int cycle = Convert.ToInt32(period.ParamValue);
                string field = this.paramPreFix + period.ParamName;
                double item = Common.CalcuteMovingAvg(r, cycle, field, base.SourceDic["Close"], curValue, base.Crab.DataSource);
                numArray[i + 1] = item;
                list.Add(item);
                num += item;
            }
            num /= (double) base.IndPeriod.Count;
            numArray[0] = num;
            return numArray;
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.tlsBbi };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.TlsBbi);
            for (int i = 0; i < base.IndPeriod.Count; i++)
            {
                IndicatorPeriod period = base.IndPeriod[i];
                base.Crab.DataSource.Columns.Remove(this.paramPreFix + period.ParamName);
            }
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                foreach (string str in base.SourceDic.Values)
                {
                    if ((str == target) || (target == null))
                    {
                        foreach (string str2 in base.SourceDic.Values)
                        {
                            if (row[str2] is DBNull)
                            {
                                return false;
                            }
                        }
                        double[] numArray = this.Get(r);
                        base.Crab.SetValue(this.TlsBbi.Name, numArray[0], r);
                        for (int i = 0; i < base.IndPeriod.Count; i++)
                        {
                            IndicatorPeriod period = base.IndPeriod[i];
                            base.Crab.SetValue(this.paramPreFix + period.ParamName, numArray[i + 1], r);
                        }
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            this.TlsBbi = base.Crab.AddLine(fields[0], panelID);
            base.Crab.SetLine(fields[0], fields[0], Color.White, 1, 0.01, 2, DashStyle.Solid);
            base.SourceDic["Close"] = source[0];
            foreach (IndicatorPeriod period in base.IndPeriod)
            {
                base.Crab.DataSource.Columns.Add(this.paramPreFix + period.ParamName);
            }
        }

        public string ParamPreFix
        {
            get
            {
                return this.paramPreFix;
            }
            set
            {
                this.paramPreFix = value;
            }
        }

        public LineShape TlsBbi
        {
            get
            {
                return this.tlsBbi;
            }
            set
            {
                this.tlsBbi = value;
            }
        }
    }
}

